package com.minesweeper;

import javafx.animation.Animation;
import javafx.animation.KeyFrame;
import javafx.animation.Timeline;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
import javafx.util.Duration;

public class GameView {
    private Stage primaryStage;
    private GameBoard gameBoard;
    private Label timeLabel;
    private Label mineLabel;
    private Timeline timeline;
    private int elapsedSeconds = 0;
    private Button[][] buttons;

    public GameView(Stage primaryStage, GameBoard gameBoard) {
        this.primaryStage = primaryStage;
        this.gameBoard = gameBoard;
        gameBoard.addGameOverListener(this::handleGameOver);
    }

    public void show() {
        GridPane gridPane = new GridPane();
        gridPane.setPadding(new Insets(10));
        gridPane.setHgap(5);
        gridPane.setVgap(5);

        buttons = new Button[gameBoard.getRows()][gameBoard.getCols()];
        for (int i = 0; i < gameBoard.getRows(); i++) {
            for (int j = 0; j < gameBoard.getCols(); j++) {
                buttons[i][j] = new Button();
                buttons[i][j].setMinSize(30, 30);
                final int row = i;
                final int col = j;
                buttons[i][j].setOnAction(e -> {
                    gameBoard.handleClick(row, col);
                    updateBoard();
                });
                buttons[i][j].setOnMouseClicked(e -> {
                    if (e.getButton().toString().equals("SECONDARY")) {
                        gameBoard.handleRightClick(row, col);
                        mineLabel.setText("剩余雷数: " + gameBoard.getRemainingMines());
                        updateBoard();
                    }
                });
                gridPane.add(buttons[i][j], col, i);
            }
        }

        timeLabel = new Label("时间: 0 秒");
        mineLabel = new Label("剩余雷数: " + gameBoard.getRemainingMines());

        Button backButton = new Button("返回主菜单");
        backButton.setOnAction(e -> {
            stopTimer();
            MainMenu mainMenu = new MainMenu(primaryStage);
            mainMenu.show();
        });

        HBox infoBox = new HBox(10);
        infoBox.getChildren().addAll(timeLabel, mineLabel, backButton);

        VBox vbox = new VBox(10);
        vbox.setPadding(new Insets(10));
        vbox.getChildren().addAll(new Label("欢迎来到扫雷游戏！"), infoBox, gridPane);

        Scene scene = new Scene(vbox, gameBoard.getCols() * 35, gameBoard.getRows() * 35 + 50);
        primaryStage.setScene(scene);
        primaryStage.show();

        startTimer();
    }

    private void startTimer() {
        timeline = new Timeline(new KeyFrame(Duration.seconds(1), e -> {
            elapsedSeconds++;
            timeLabel.setText("时间: " + elapsedSeconds + " 秒");
        }));
        timeline.setCycleCount(Animation.INDEFINITE);
        timeline.play();
    }

    private void stopTimer() {
        if (timeline != null) {
            timeline.stop();
        }
    }

    private void handleGameOver(boolean isWin) {
        stopTimer();
        if (isWin) {
            WinDialog.show(elapsedSeconds);
        } else {
            LoseDialog.show();
        }
        updateBoard();
    }

    private void updateBoard() {
        for (int i = 0; i < gameBoard.getRows(); i++) {
            for (int j = 0; j < gameBoard.getCols(); j++) {
                updateCell(i, j);
            }
        }
    }

    private void updateCell(int row, int col) {
        if (gameBoard.isRevealed(row, col)) {
            if (gameBoard.isMine(row, col)) {
                buttons[row][col].setText("\uD83D\uDCA3");
                buttons[row][col].setStyle("-fx-background-color: red;");
            } else {
                int number = gameBoard.getNumber(row, col);
                if (number > 0) {
                    buttons[row][col].setText(String.valueOf(number));
                } else {
                    buttons[row][col].setDisable(true);
                    buttons[row][col].setText("");
                }
            }
        } else if (gameBoard.isFlagged(row, col)) {
            buttons[row][col].setText("🚩");
        } else {
            buttons[row][col].setText("");
        }
    }
}